Function Parameter Passing

 

Passing by value

 

When passing by value, the original value in the calling program does not change. If you want to do something to the resultant value in the calling function, you must pass by reference or use a function that returns a value.

 

void Triple(int);          // Function's prototype Note the

                                   // absence of variable names

                                   // in the parameter list and

                                   // an ending semi-colon

 

void main(void)

{

int Number = 10;     // declares a variable called

                                 // Number and sets it to 10

 

Triple(Number);          // Passes the data in the

                                    // variable Number to the

                                    // function calIed Triple

cout « "The Number in main is stilI "

        « Number « endl;

 

}     // This is the end of function main

 

 

void Triple (int FunctionsNumber)

{

cout « "The triple of" « FunctionsNumber

                    « " is " « FunctionsNumber * 3 « endl;

 

}     // End of function Triple

 


Passing by reference

 

To do nearly the same thing, only this time have an original value changed by the function, we will pass a variable by reference. In this example we will use a second variable in the parameter list so that we do not destroy the original contents of Number.

 

void Triple(int, int&);      // Function's prototype Note the

                                        // absence of variable names

                                        // in the parameter list and an

                                        // ending semi-colon

 

void main(void)

{

int Number = 10;     // declares a variable called

                                 // Number and sets it to 10

int Result=9999;     // declares a variable Result

                                //  and sets it to 9999 just

                                // to prove a point

    

Triple(Number, Result);     // Passes the data in the

                                           // variable Number to

                                           // the function. The value

                                           // of result is also passed

cout « "The Number in main is still" « Number « endl;

cout « "The Result in main is " « Result « endl;

 

}     // This is the end of function main

 

 

void Triple (int FunctionsNumber, int& FunctionsResu1t)

{

FunctionsResult = FunctionsNumber * 3 ;

cout « "The triple of" « FunctionsNumber

« " is " « FunctionsResult « endl;

 

}     // end of function Triple

 

As you can see, the cout statement in the function Triple is no longer needed since we have the same information in the function main. It was left here as part of the explanation of how this function works.

 


Returning a single value

 

The final function method allows us to return a value from a function. Unlike the other methods that use data that are stored in variables, such as Number, Result, etc. this method creates a new value that must be used somehow in the calling function. It must be assigned to a variable, used in a cout, used in a numeric expression, used in a logical expression, etc. Since this method is returning data from the function, the function' s return data type cannot be void. It must now indicate the type of data that is returned.

 

int Triple(int);           // Function's prototype. Note the

                                // absence of variable names

                                // in the parameter list and a

                               // semi-colon after the statement.

                                // Note that the function' s return

                               // data type is now int.  There is

                               //  no limit to the number of

                               // parameters you can pass

                               // to the function, only the number

                               // that can be returned; only

                               // one value can be returned.

 

void main(void)

{

int Number = 10;     // declares a variable called

                                 // Number and sets it to 10

int Result;                // declares a variable in which

                                //  we will store the value

                                //  that is returned from the

                                //  function

 

Result=Triple(Number) ;     // Passes the data in the

                                          // variable Number

                                          // and stores the returned

                                          // value in Result.

 

cout « "The Number in main is still" « Number « endl;

cout « "The Result in main is " « Result « endl.

 

}     // This is the end of function main


int Triple (int FunctionsNumber)

{

FunctionsResult = FunctionsNumber * 3 ;

cout « "The triple of" « FunctionsNumber

« " is " « FunctionsResult « endl;

 

return FunctionsResult;     // This is the statement

                                            //  that makes this type

                                            //  of function different.

                                            //  Remember only

                                            //  one value can be

                                            //  returned

 

}          // End of function Triple

 

As you can see, the cout statement in the function Triple is no longer needed since we have the same information in the function main. It was left here as part of the explanation of how this function works.


Summary

 

The question now becomes which method should you use for a given application.

 

If you do not want an original variable in the calling function to possibly be changed by the called function, use passing by value instead of passing by reference.

 

If you need to have more than one value pass back from a function, your only choice is to pass by reference. Since only one variable can be returned from a function as in method 3, if you need to have multiple values "returned", method 3 will not work.

 

Beyond those rules, since you can do basically the same thing in more than one way (isn't the effect of method 2 and method 3 the same?), there is no simple answer. Use whichever method you are most comfortable with.